package vn.orderonline.web.client.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.orderonline.com.core.business.category.ICategory;
import vn.orderonline.com.core.business.common.ICommon;
import vn.orderonline.com.core.business.dto.AreaDto;
import vn.orderonline.com.core.business.dto.RestaurantDto;
import vn.orderonline.com.core.business.dto.ToOrderDto;
import vn.orderonline.com.core.business.restaurant.IRestaurantManager;
import vn.orderonline.com.core.business.temporaryorder.ITemporaryDrinkBasket;
import vn.orderonline.com.core.business.temporaryorder.ITemporaryFoodBasket;
import vn.orderonline.com.core.business.toorder.IToOrder;
import vn.orderonline.core.commons.utils.StringUtils;

@Controller
@RequestMapping(value = "/client")
public class ClientController {

	private static final Logger LOGGER = Logger
			.getLogger(ClientController.class);

	@Autowired
	private ICommon iCommon;
	@Autowired
	private ICategory iCategory;
	@Autowired
	private IRestaurantManager iRestaurantManager;
	@Autowired
	private IToOrder iToOrder;
	@Autowired
	private ITemporaryFoodBasket iTemporaryFoodBasket;
	@Autowired
	private ITemporaryDrinkBasket iTemporaryDrinkBasket;

	@RequestMapping(value = "/choose-area", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String chooseArea(ModelMap model) {
		LOGGER.info("START GO TO CHOOSE AREA PAGE!");
		try {
			model.addAttribute("area", new AreaDto());
			model.addAttribute("cityList", iCommon.cityList());

		} catch (Exception e) {
			LOGGER.info("ERROR WHEN GO TO CHOOSE AREA PAGE!", e);
		}
		LOGGER.info("END GO TO CHOOSE AREA PAGE!");
		return "/client/choose-area";
	}

	@RequestMapping(value = "/choose-restaurant", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String chooseRestaurant(@ModelAttribute AreaDto area, ModelMap model) {
		LOGGER.info("START GO TO CHOOSE RESTAURANT!");
		try {
			area.setCityName(iCommon.getCityNameByCityId(area.getCityId()));
			area.setDistrictName(iCommon.getDistrictNameByDistrictId(area
					.getDistrictId()));
			model.addAttribute("area", area);
			model.addAttribute(
					"size",
					iRestaurantManager.findRestaurantByCityIdAndDeliverHome(
							area.getCityId(), area.getDistrictId().toString())
							.size());
			model.addAttribute("categoryList", iCategory.getListCategory());
			model.addAttribute("list", iRestaurantManager
					.findRestaurantByCityIdAndDeliverHome(area.getCityId(),
							area.getDistrictId().toString()));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN GO TO CHOOSE RESTAURANT!", e);
		}
		LOGGER.info("END GO TO CHOOSE RESTAURANT!");
		return "/client/choose-restaurant";
	}

	@RequestMapping(value = "/choose-food", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public String chooseFood(@RequestParam Integer restaurantId, ModelMap model) {
		LOGGER.info("START GO TO CHOOSE FOOD!");
		try {
			RestaurantDto res = iRestaurantManager.getRestaurantById(restaurantId, "detail");
			model.addAttribute("restaurant", res);
			model.addAttribute("listFood",
					iRestaurantManager.chooseFood(restaurantId));
			model.addAttribute("listDrink",
					iRestaurantManager.chooseDrink(restaurantId));
			model.addAttribute("lf", iTemporaryFoodBasket.getAllFoods());
			model.addAttribute("ld", iTemporaryDrinkBasket.getAllDrinks());
			model.addAttribute("count", iTemporaryFoodBasket.calculateFoodPrice() + iTemporaryDrinkBasket.calculateDrinkPrice() + res.getDeliveryPrice());
			model.addAttribute("menu",iRestaurantManager.getMenu(restaurantId));
		} catch (Exception e) {
			LOGGER.info("ERROR WHEN GO TO CHOOSE FOOD!");
		}
		LOGGER.info("END GO TO CHOOSE FOOD!");
		return "/client/choose-food";
	}

	@RequestMapping(value = "/count-money", method = RequestMethod.GET)
	public String countMoney(ModelMap model, @RequestParam Integer restaurantId) {
		LOGGER.info("START COUNT MONEY!");
		try {
			RestaurantDto res = iRestaurantManager.getRestaurantById(restaurantId, "detail");
			model.addAttribute("restaurant", res);
			model.addAttribute("lf", iTemporaryFoodBasket.getAllFoods());
			model.addAttribute("ld", iTemporaryDrinkBasket.getAllDrinks());
			model.addAttribute("count", iTemporaryFoodBasket.calculateFoodPrice() + iTemporaryDrinkBasket.calculateDrinkPrice() + res.getDeliveryPrice());
			model.addAttribute("dto", new ToOrderDto());
		} catch (Exception e) {
			LOGGER.error("ERROR COUNT MONEY!", e);
		}
		LOGGER.info("END COUNT MONEY!");
		return "/client/count-money";
	}
	
	@RequestMapping(value = "/toorder", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public String toOrder(@ModelAttribute ToOrderDto dto, ModelMap model) {
		LOGGER.info("START TO ORDER!");
		try {
			String orderCode = iToOrder.toOrder(dto);
			model.addAttribute("orderCode", orderCode);
			model.addAttribute("resName", iRestaurantManager.getRestaurantNameByRestaurantId(dto.getRestaurantId()));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO ORDER!");
		return "/client/success";
	}

	@RequestMapping(value = "/getdistrictbycityid", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getDistrictByCityId(@RequestParam Integer cityId) {
		LOGGER.info("START GET DISTRICT BY CIATY ID!");
		String str = "";
		try {
			str = StringUtils.parseToJSON(iCommon.districtList(cityId));
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN GET DISTRICT BY CITY ID!", e);
		}
		LOGGER.info("END GET DISTRICT BY CITY ID!");
		return str;
	}

	@RequestMapping(value = "/addfood", method = RequestMethod.POST, produces = { "appliction/json;charset=UTF-8" })
	@ResponseBody
	public String addFood(@RequestParam Integer restaurantId,
			@RequestParam Integer foodId, @RequestParam String foodName,
			@RequestParam Float price) {
		LOGGER.info("START ADD NEW FOOD TO DB!");
		try {
			iTemporaryFoodBasket.addFood(restaurantId, foodId, foodName, price);
		} catch (Exception e) {
			LOGGER.info(e.getMessage(), e);
		}
		LOGGER.info("END ADD FOOD TO DB!");

		return "" + foodId;
	}

	@RequestMapping(value = "/adddrink", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String addDrink(@RequestParam Integer restaurantId,
			@RequestParam Integer drinkId, @RequestParam String drinkName,
			@RequestParam Float price) {
		LOGGER.info("START ADD NEW DRINK TO DB!");
		try {
			iTemporaryDrinkBasket.addDrink(restaurantId, drinkId, drinkName,
					price);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END ADD NEW DRINK TO DB!");
		return "" + drinkId;
	}
	
	@RequestMapping(value = "/removefoodbasket", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String removeFoodBasket(@RequestParam Integer restaurantId, @RequestParam Integer foodId) {
		LOGGER.info("START REMOVE FOOD BASKET!");
		try {
			iTemporaryFoodBasket.removeFoodBasket(restaurantId, foodId);
		} catch (Exception e) {
			LOGGER.info(e.getMessage(), e);
		}
		LOGGER.info("END REMOVE FOOD BASKET!");
		return "" + restaurantId + foodId;
	}
	
	@RequestMapping(value = "/removedrinkbasket", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String removeDrinkBasket(@RequestParam Integer restaurantId, @RequestParam Integer drinkId) {
		LOGGER.info("START REMOVE DRINK BASKET!");
		try {
			iTemporaryDrinkBasket.removeDrinkBasket(restaurantId, drinkId);
		} catch (Exception e) {
			LOGGER.info(e.getMessage(), e);
		}
		LOGGER.info("END REMOVE DRINK BASKET!");
		return "" + restaurantId + drinkId;
	}
}
